package com.meizi.user.service.config;

import com.meizi.core.tool.api.R;
import com.meizi.core.tool.exception.BaseException;
import com.meizi.core.tool.exception.BizException;
import com.meizi.core.tool.exception.ValidException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @author meizi
 * @email enjoymeizi@gmail.com
 * @date 2020/11/27 17:36
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public R handleException(Exception e, HttpServletRequest request) {
        log.info("GlobalExceptionHandler.handleException:{},{}", request.getRequestURI(), e);
        if (e instanceof ValidException || e instanceof BizException) {
            BaseException be = (BaseException) e;
            return new R().of(StringUtils.isBlank(be.getCode()) ? -2 : Integer.valueOf(be.getCode()), be.getMsg(), null);
        }
        return new R().of(-1, e.getMessage(), null);
    }
}
